package com.zt.ftphelper;

import com.zt.ftphelper.view.FileSelectController;
import com.zt.ftphelper.view.RootLayoutController;
import com.zt.ftphelper.view.SettingsController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {

    private Stage primaryStage;
    private BorderPane rootLayout;
    private RootLayoutController rootLayoutController;
    private FileSelectController fileSelectController;

    public Main() {
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;
        initRootLayout();
        showFileSelect();
    }

    public static void main(String[] args) {
        launch(args);
    }

    public void initRootLayout() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
            rootLayout = loader.load();
            rootLayoutController = loader.getController();
            rootLayoutController.setMain(this);
            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setTitle("FtpHelper");
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void showFileSelect() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("view/FileSelect.fxml"));
            AnchorPane fileSelect = loader.load();
            rootLayout.getBottom().maxHeight(30);
            rootLayout.setCenter(fileSelect);
            fileSelectController = loader.getController();
            fileSelectController.setMain(this);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void showSettingsDialog(){
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("view/Settings.fxml"));
            AnchorPane page = (AnchorPane) loader.load();

            // Create the dialog Stage.
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Edit Settings");
            dialogStage.setResizable(false);
            dialogStage.initModality(Modality.WINDOW_MODAL);
            dialogStage.initOwner(primaryStage);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);

            // Set the person into the controller.
            SettingsController controller = loader.getController();
            controller.setDialogStage(dialogStage);
            controller.setMain(this);
            // Show the dialog and wait until the user closes it
            dialogStage.showAndWait();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setFooter(String text) {
        rootLayoutController.setFooter(text);
    }

    public void refreshTree() {
        TreeView treeView = fileSelectController.buildTree();
        treeView.getSelectionModel().select(treeView.getRoot());
    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }

}
